function BBCodes(){
	var el;
	this.setArea = function(area){
		this.el = document.getElementById(area);
		this.el.focus();
	}
	this.doInsert = function (startTag, endTag) {
		var el = this.el;

		var scrltop  = el.scrollTop;
		var selstart = el.selectionStart;
		var selend   = el.selectionEnd;

		var start  = (el.value).substring(0, selstart);
		var middle = (el.value).substring(selstart, selend);
		var end    = (el.value).substring(selend, el.textLength);

		el.value = start + startTag + middle +endTag + end;

		var selpos = selstart + middle.length + startTag.length + endTag.length;

		el.selectionStart = selpos;
		el.selectionEnd   = selpos;
		el.scrollTop      = scrltop;
        el.focus();
	}
	this.simpleTag = function(thetag){
		this.doInsert("["+thetag+"]","[/"+thetag+"]");
	}
	this.smile = function (smile){
		this.doInsert('::'+smile+'::','');
	}
	this.imagetag = function (thetag){
		var el = this.el;
		var selstart = el.selectionStart;
		var selend   = el.selectionEnd;
		if ((selend)-(selstart) == 0){
			var v1 = prompt(lang['imagePath'], "http://");
			this.doInsert("["+thetag+"]" + v1,"[/"+thetag+"]");
		}else{
			this.doInsert("["+thetag+"]","[/"+thetag+"]");
		}
	}
	this.url_tag = function(thetag){
		var el = this.el;
		var v1=prompt(lang['url'], 'http://');
		var v2=prompt(lang['urlName'], '');
		if (v2 == ''){v2 = v1;}
		this.doInsert('['+thetag+'='+v1+']'+v2+'[/'+thetag+']','');
	}
	this.init = function(){
		$('textarea').click(function(){
			BB.setArea( $(this).attr('id') );
		});
	}
}

BB = new BBCodes();
$(document).ready(function(){
	BB.init();
});